import {showToast} from '../../utils/asyncWx'
Page({
  data: {
    address:{},
    cart:[],
    allSelected:false,
    totalPrice:0,
    totalNum:0
  },
  onShow() {
    //获取缓存中的收货地址数据
    let address = wx.getStorageSync('address');
    //获取缓存中的购物车数据
    let cart = wx.getStorageSync('cart') || [];
    //单独设置address
    this.setData({
      address
    });
    this.setCart(cart);
  },
  handleChooseAddress() {
    //调用微信内置的api获取收货地址
    wx.chooseAddress({
      success: (result) => {
        wx.setStorageSync('address', result)
      },
    })
  },
  //改变select的选项
  handleChangeSelect(e) {
    //通过点击的商品，把该商品的data和缓存中的值改变
    //1.取出商品的id值
    let {id} = e.currentTarget.dataset;
    //找到数组中的该商品，然后修改
    let {cart} = this.data;
    let currentGood = cart.find((item) => id === item.goods_id);
    //将商品的选择状态修改
    currentGood.isSelected = !currentGood.isSelected;
    //写回data数据中
    // this.setData({
    //   cart
    // });
    console.log(cart);
    //设置缓存
    wx.setStorageSync('cart', cart);
    this.setCart(cart);
  },
  //
  handleAllSelect() {
    // console.log('hello world');
    //先拿到全选的值
    let {cart,allSelected} = this.data;
    allSelected = !allSelected;
    //改变商品数组中所有的选中状态
    cart.forEach(item => item.isSelected = allSelected);
    this.setData({
      cart
    });
    this.setCart(cart);
  },
  //加减数量
  handleItemNumbleEdit(e) {
    //取出goods_id 和 type
    const {id:goods_id,type} = e.currentTarget.dataset;
    let {cart} = this.data;
    //找到需要修改的商品
    var currentGood = cart.find(item => item.goods_id === goods_id);
    let goodIndex = cart.findIndex(item => item.goods_id === goods_id);
    if(currentGood.num === 1 && type === -1) {
      // wx.showToast({
      //   title: '不能在减了哦',
      //   duration: 2000
      // })
      wx.showModal({
        title: '删除',
        content: '确定要删除该商品吗',
        success: res => {
          if (res.confirm) {
            cart.splice(goodIndex,1);
            //写回data
            this.setCart(cart);
            //写回缓存
            wx.setStorageSync('cart', cart);
          } else if (res.cancel) {
            currentGood.num = 1;
            console.log('用户点击取消')
          }
        }
      })
    }else {
      currentGood.num += type;
      //写回data
      this.setCart(cart);
      //写回缓存
      wx.setStorageSync('cart', cart);
    }

  },
  //结算功能实现
  async handlePay() {
    
    const {address,totalNum} = this.data;
    //1.判断地址存不存在
    if(!address.userName) {
      await showToast('请填写你的收货地址')
      return;
    }
    //判断是否选购了商品
    // if(totalNum == 0) {
    //   await showToast('请先去选购商品');
    //   return;
    // }
    //如果走到这里说明选了商品和地址
    wx.navigateTo({
      url: '/pages/pay/index',
    })
  },

  //设置购物车状态 包括钱，数量，
  setCart(cart) {
    //计算是不是全选
    let allSelected = cart.length ==0 ? false : true;
    //计算总价格
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(item => {
      if(item.isSelected) {
        totalPrice += item.goods_price * item.num;
        totalNum += item.num;
      }else {
        allSelected = false;
      }
    });
    this.setData({
      cart,
      allSelected,
      totalPrice,
      totalNum
    })
  }
})